import { createRouter, createWebHistory } from 'vue-router'
import vueCookies from 'vue-cookies';

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'Layout',
      component: () => import('@/views/Layout.vue'),
    },
    {
      path: '/loign',
      name: 'Loign',
      component: () => import('@/views/Login.vue'),
    },
    {
      path: '/qqlogincallback',
      name: 'qq登录回调',
      component: () => import('@/views/qqLoginCallback.vue'),
    },
  ],
})

router.beforeEach((to, from, next) => {
  const userInfo = vueCookies.get('userInfo');
  if (to.meta.needLogin == null && to.meta.needLogin && userInfo == null) {
    router.push('/login')
  }
  next();
})

export default router
